﻿using System.Windows;
using SLS.Core.Workspace;
using SLS.Controls;
using System.Windows.Input;
using System.Data;
using SLS.ExClassLib.Database;
using Cinch;
namespace SLS.Presentation
{
    /// <summary>
    /// Startup dialog is displayed at the application launch after splash and before application main window
    /// </summary>
    [PopupNameToViewLookupKeyMetadata("UILogOn", typeof(UILogOn))]
    public partial class UILogOn : Window
	{
		#region ---------------------DLG---------------------

		/// <summary>
		/// Constructor
		/// </summary>
        public UILogOn()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(UILogOn_Loaded);
            this.txtUser.Text = "LANSUN";
            this.txtPassword.Password = "1";
            this.cmbAgent.Text = "1";
            this.cmbSystem.Text = "1";
       }

        void UILogOn_Loaded(object sender, RoutedEventArgs e)
        {
            btnSignIn_Click(null, null);
        }
        public DataRow dwUser; 
		#endregion

		#region ---------------------PROPERTIES---------------------
		/// <summary>
		/// the command
		/// </summary>
		private static RoutedCommand _StartupCmd = new RoutedCommand("StartupCommand", typeof(UILogOn));

		public static RoutedCommand StartupCmd
		{
			get { return UILogOn._StartupCmd; }
			set { UILogOn._StartupCmd = value; }
		}

		#endregion   
        private void btnSignIn_Click(object sender, RoutedEventArgs e)
        {
            string UserName = this.txtUser.Text.ToUpper().ToString().Trim();
            string strPassword = this.txtPassword.Password.ToString().Trim();
            strPassword = "1";
            string SM = this.cmbSystem.Text.ToUpper().ToString().Trim();
            string AgentID = this.cmbAgent.Text.ToUpper().ToString().Trim();
            dwUser =DataAccessService.Instance.GlobalDataSet.UserManagement.NewRow();
            DataTable TBUserInformation = DataAccessService.Instance.GlobalDataSet.UserManagement;
            var query = from dwUserInformation in TBUserInformation.AsEnumerable()
                        where dwUserInformation["USERNAME"].ToString().TrimEnd().ToUpper() == UserName.Trim()
                        && dwUserInformation["PASSWORD"].ToString().Trim() == strPassword.Trim()
                            && dwUserInformation["SM"].ToString().Trim() == SM.Trim() 
                        select dwUserInformation;

            foreach (DataRow dwUserInformation in query)
            {
                dwUser["SM"] = dwUserInformation["SM"].ToString().Trim();
                dwUser["USERNAME"] = dwUserInformation["USERNAME"].ToString().Trim().ToUpper();
                dwUser["AGENTID"] = AgentID;
                if (chkDesign.IsChecked == true)
                {
                    dwUser["ISDESIGNTIME"] = "Yes";
                }
                else
                {
                    dwUser["ISDESIGNTIME"] = "No";
                }

                if (chkTrace.IsChecked == true)
                {
                    dwUser["ISTRACE"] = "Yes";
                    DataAccessService.Instance.GlobalDataSet.Tables["AgentList"].Rows[0]["Status"] = "Trace";
                }
                else
                {
                    dwUser["ISTRACE"] = "No";
                    DataAccessService.Instance.GlobalDataSet.Tables["AgentList"].Rows[0]["STATUS"] = "No";
                }
                DataAccessService.Instance.GlobalDataSet.Tables["UserManagement"].Rows.Clear();
                //dsLogin=UIHelp.ImportDataTable(dsLogin, dwUser);
                DataAccessService.Instance.GlobalDataSet.Tables["UserManagement"].AcceptChanges();
                this.Close();
                StartupCmd.Execute(
                new AgentParameters(eAgentCommand.LogIn,dwUser), Application.Current.MainWindow);
                return;
            }
            MessageBox.Show("请输入登录正确的输入信息");
        } 
    }

	/// <summary>
	/// Possible startup command options
	/// </summary>
	public enum eAgentCommand
	{
		/// <summary>
		/// create a new project by showing the backstage
		/// </summary>
		New,
		/// <summary>
		/// open a project by showing the open dialog
		/// </summary>
		Open,
		/// <summary>
		/// open a given project directly
		/// </summary>
		QuickOpen,
		/// <summary>
		/// display help
		/// </summary>
		Help,
		/// <summary>
		/// display technical help
		/// </summary>
		HelpTechnical,
		/// <summary>
		/// open sample
		/// </summary>
		LogIn
	}

	/// <summary>
	/// Used to transfer information with the StartupCommand
	/// </summary>
	public class AgentParameters
	{
		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="cmd"></param>
        public AgentParameters(eAgentCommand cmd)
		{
			Command = cmd;
			File = string.Empty;
            dwUser = null;
		}

		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="cmd"></param>
		/// <param name="file"></param>
        public AgentParameters(eAgentCommand cmd, string file)
		{
			Command = cmd;
			File = file;
		}
        public AgentParameters(eAgentCommand cmd, DataRow dwUser)
        {
            Command = cmd;
            File = string.Empty;
            this.dwUser = dwUser;
        }

		/// <summary>
		/// Associated command option
		/// </summary>
        public eAgentCommand Command
		{
			get;
			set;
		}

		/// <summary>
		/// Associated filename
		/// </summary>
		public string File
		{
			get;
			set;
		}
        public DataRow dwUser
        {
            get;
            set;
        }
	}
}
